﻿$.extend({
    //写入等待
    ShowMask: function () {
        var body = $("body").eq(0);
        var mask = $("<div class=\"datagrid-mask\" style=\"display:block; z-index:77777;\"></div>").appendTo(body);
        var img = $("<img class=\"datagrid-mask-img\" src='images/loading.gif' style=\"z-index:88888;\" />");
        img.css("left", (body.width() - 214) / 2).css("top", 12).css("position", "absolute").appendTo(body);
    },
    //移除等待
    HideMask: function () {
        var body = $("body").eq(0);
        body.children("div.datagrid-mask").remove();
        body.children("img.datagrid-mask-img").remove();
    },
    //Api路径
    Api: function (path) {
        return "../public/ApiHandler.ashx?path=" + path;
    },
    //Ajax遮罩
    AjaxMask: function (options) {
        $.ajax({
            url: options.url, type: options.type,
            data: options.data, success: options.success,
            beforeSend: function () { $.ShowMask(); },
            complete: function () { $.HideMask(); },
            error: function (request, status, thrown) { $.Error(request.responseText); }
        });
    },
    //报错
    Error: function (html) {
        html = String(html).replace(/<style[^>]*?>[\s\S]*?<\/style>/, '');
        html = html.replace(/<meta[^>]*?>[\s\S]*?>/, '');
        html = html.replace(/<title[^>]*?>[\s\S]*?<\/title>/, '');
        html = $("<div style='padding:15px;'>" + html + "</div>");
        
        $.AdjustmentDialog($.ModelDialog({ title: "错误", width: 600, content: html.show() }));
    },
    //刷新列表
    DataGridReload: function (value) {
        $(value).datagrid("reload");
        $(value).datagrid("clearChecked");
    },
    //刷新树表
    TreeGridReload: function (value) {
        $(value).treegrid("reload");
        $(value).treegrid("unselectAll");
    },
    //展开节点
    TreeGridExpandAll: function (value) {
        $(value).treegrid("expandAll");
    },
    //收起节点
    TreeGridCollapseAll: function (value) {
        $(value).treegrid("collapseAll");
    },
    //树形全选
    TreeCheckAll: function (value) {
        var nodes = $(value).tree("getChecked", "unchecked");
        for (var i = 0; i < nodes.length; i++) { $(value).tree("check", nodes[i].target); }
    },
    //取消选择
    TreeUnCheckAll: function (value) {
        var nodes = $(value).tree("getChecked");
        for (var i = 0; i < nodes.length; i++) { $(value).tree("uncheck", nodes[i].target); }
    },
    //提示信息
    EasyuiMsg: function (content) {
        $.messager.alert("提示", content, "info");
    },
    //浮出信息
    EasyuiSlide: function (content) {
        $.messager.show({ title: "提示", msg: content });
    },
    //确认提示
    EasyuiConfirm: function (content, handler) {
        $.messager.confirm("提示", content, handler);
    },
    //获取图标
    GetIcon: function (value) {
        return "<span class='list-span " + value + "'></span>";
    },
    //获取按钮
    GetIconButton: function (value, text, fun) {
        var span = "<span class='l-btn-text " + value + " l-btn-icon-left'>" + text + "</span>";
        var spanbox = "<span class='l-btn-left'>" + span + "</span>";
        return "<a class='list-link' href='javascript:void(0)' onclick='" + fun + "'>" + spanbox + "</a>";
    },
    //获取播放器
    GetDewplayer: function (file, span) {
        var player = $("<div />");
        player.html('<embed src="../public/dewplayer/dewplayer.swf?mp3=' + file + '"' +
                        'type="application/x-shockwave-flash"' +
                        'bgcolor="#ffffff" quality="high" wmode="transparent"' +
                        'allowfullscreen="true" allowscriptaccess="always" width="200" height="20"' +
                        'pluginspage="http://www.macromedia.com/go/getflashplayer" />');
        if (($.browser.msie && Number($.browser.version) < 8) || !span) { return player.html(); }
        else { return "<span style='display:inline-block; height:20px;vertical-align:middle;'>" + player.html() + "</span>"; }
    },
    //获取是否
    GetYesOrNo: function (value) {
        if (value) {
            return $.GetIcon("icon-bullet_tick");
        }
        else { return $.GetIcon("icon-bullet_minus"); }
    },
    //获取是否
    GetYesOrNoData: function () {
        return {
            editable: false, required: true,
            width: 150, panelHeight: "auto",
            data: [
                { value: "True", text: "是" },
                { value: "False", text: "否" }
            ]
        };
    },
    //实体窗口
    ModelDialog: function (options) {
        if (options.dialogid) {
            options.dialogid = "id='" + options.dialogid + "'";
        }
        var buttons = [];
        if (!options.width) { options.width = "auto"; }
        if (!options.height) { options.height = "auto"; }
        
        if (options.handler) { buttons.push({ text: "确定", iconCls: "icon-ok", handler: options.handler }); }
        buttons.push({ text: "关闭", iconCls: "icon-no", handler: function () { $.ButtonCloseDialog(this); } });

        var dialog = $("<div " + options.dialogid + " />").dialog({
            title: options.title, href: options.href,
            width: options.width, height: options.height,
            toolbar: options.toolbar, content: options.content,
            modal: true, cache: false, collapsible: true,
            buttons: buttons, iconCls: options.iconCls,
            onLoad: function () {
                options.onLoad(); $.AdjustmentDialog(dialog);
            },
            onClose: function () { $(this).dialog("destroy"); }
        });
        return dialog;
    },
    //调整位置
    AdjustmentDialog: function (dialog) {
        var boxheight = $(dialog).parent().height();
        if (boxheight > $(window).height() - 20) {
            dialog.dialog("resize", {
                height: $(window).height() - 20
            });
            $(dialog).parent().next().height($(window).height() - 20);
        }
        else { $(dialog).parent().next().height(boxheight + 12); }
        dialog.dialog("center");
    },
    //关闭窗口
    ButtonCloseDialog: function (button) {
        $(button).closest(".window-body").dialog("destroy");
    },
    //表单实体
    ModelForm: function (options) {
        var submit = function () {
            var bool = $(this).form("validate");
            if (options.editor) {
                KindEditor.sync(options.editor);
            }
            if (bool) bool = options.validate;
            if (bool) $.ShowMask(); return bool;
        }
        $(options.id).form("submit", {
            url: options.url, onSubmit: submit,
            success: function (result) {
                $.HideMask();
                try {
                    result = JSON.parse(result);
                    if (result.Success) {
                        $.EasyuiSlide(result.Message);
                        options.success(result);
                        $.ButtonCloseDialog(options.button);
                    }
                    else { $.EasyuiMsg(result.Message); }
                } catch (e) { $.Error(result); }
            }
        });
    },
    //编辑器初始化
    EditorInit: function (textid, height, lingtag, filterMode) {
        var editorkey = KindEditor.create(textid, {
            uploadJson: "../public/kindeditor/FilesUpload.ashx",
            fileManagerJson: "../public/kindeditor/FilesManager.ashx",
            width: "auto", height: height, allowFileManager: true,
            newlineTag: lingtag, filterMode: filterMode, resizeType: 0,
            items: [
                'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', 'clearhtml', 'quickformat', 'selectall', '/',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
                'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
                'anchor', 'link', 'unlink'
            ]
        });
        prettyPrint(); return editorkey;
    },
    //迷你编辑器初始化
    MiniEditorInit: function (textid, height, lingtag, filterMode) {
        var editorkey = KindEditor.create(textid, {
            uploadJson: "../public/kindeditor/FilesUpload.ashx",
            fileManagerJson: "../public/kindeditor/FilesManager.ashx",
            width: "auto", height: height, allowFileManager: true,
            newlineTag: lingtag, filterMode: filterMode, resizeType: 0,
            items: [
                'source', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', '|', 'fontsize', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', '|', 'link', 'unlink'
            ]
        });
        prettyPrint(); return editorkey;
    }
});
